﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Sockets;
using System.Data;
using System.Configuration;

namespace YunShop_WebAPI.BLL.SxfPay
{
    public class SxfConfig
    {
        //聚合支付地址
        public static string PayServiceUrl = "https://openapi.tianquetech.com/order/jsapiScan";
        //查询地址
        public static string QueryServiceUrl = "https://openapi.tianquetech.com/query/tradeQuery";
        //退款申请地址
        public static string RefundServiceUrl = "https://openapi.tianquetech.com/order/refund";
        //退款查询地址
        public static string RefundQueryUrl = "https://openapi.tianquetech.com/query/refundQuery";

        //半屏支付地址
        public static string PayAppletScanPreUrl = "https://openapi.tianquetech.com/order/appletScanPre";


        //机构Id
        private static string _orgId = "";    //需要获取

        //商户编号
        public static string _mechId = "399190618057330";    //需要获取

        //PublicKey解密传回数据的公钥，当前测试环境使用
        //正式环境需要更换
        private static string _PublicKey = "";

        //加密用私钥
        private static string _RsaPrivateKey = @"";

        public static string orgId
        {
            get
            {
                if (!string.IsNullOrEmpty(_orgId))
                    return _orgId;
                setData();
                return _orgId;
            }
        }

        public static string RsaPrivateKey
        {
            get
            {
                if (!string.IsNullOrEmpty(_RsaPrivateKey))
                    return _RsaPrivateKey;
                setData();
                return _RsaPrivateKey;
            }
        }

        public static string PublicKey
        {
            get
            {
                if (!string.IsNullOrEmpty(_PublicKey))
                    return _PublicKey;
                setData();
                return _PublicKey;
            }
        }

        public static string getMechId(int business_id)
        {
            //string msql = string.Format("select sxf_mno from b_business_account where fk_business_id = {0}", business_id);
            string msql = string.Format("select FMerchantCode from t_ShouKuanElfin where FCorpID =  {0}", business_id);
            DataTable dt = DBHelper.GetDataTable(msql, DBHelper.ConnMaster);
            if (dt == null || dt.Rows.Count == 0) return "";
            return CommonFun.ConvertToString(dt.Rows[0]["FMerchantCode"]);
            //return "399200819168726";
            //return "399240927243670";
        }

        public static string getMiniProgramAppId(int business_id)
        {
            //string msql = string.Format("select appid from b_business_account where fk_business_id = {0}", business_id);
            //DataTable dt = DBHelper.GetDataTable(msql);
            //if (dt == null || dt.Rows.Count == 0) return "";
            //return CommonFun.ConvertToString(dt.Rows[0]["appid"]);
            //return "wx6a9774145ed30802";
            return "wxf7ce08d94685bf3e";
        }

        private static void setData()
        {
            _orgId = "36208466";
            _RsaPrivateKey = "MIICXAIBAAKBgQCylVMgMy63fvoagvUF+2NKmEM0fhDZv9CnMHB4Egt31YroEN/Rn+MoZM80seyB/3jsttBQFT763UDDuYH9RO2i2nCSqipT9WIs5sqxl9Uw209M+Tit9o7KTAyczYeLKoa3BAzsc2r2zdrQEzE5lm+wK7wiEdDqcQ1HtjslGJCJsQIDAQABAoGAF/vA7a6iBAe8DApxYKvJwm9fezHBDpVZrtJtEFsQCjh2DsORY4QvDDI5t7B7fFv0gAnvQQoRaryDIaYMxJhYt5AdDdYMwGUlgrM35L8eBwEmZnV5tHAZlDP0ceJYZS3xKaTwjMipoDJ/r75SvHyDsm3FsqxwJpBekOtal1iX32kCQQDB7BVZ11ce8uN4yeufzAsa2UFjqUqL5S6kZ2YOV08FQqQQy6SxHnZ0xCJr+TNPq3UZJormddRbryPOdtEPsoFFAkEA68A4Dp3QF4RsF+9aEwFGs3ZkIB7uFvApZ33z1zhGx/3qi/6FhE4ElWqOH63MkAdZJfshkiaSTxl9L97keFjvfQJBALrOTZMKGea6W95TxSNeT6RBfPggcKtqzw+Ye3t9J6YNmDaFtdTZX4sEFZSlroQkyty7gDqZb0QS5w9Q7fuNHXECQEOzG2X3I77dJD1rWq6Ce08l/HLM7wpfRUSXAuMXIhxkampxWN45mU9fHhkyy7/AnHUvvOA/e1yDsVOB6pI01/ECQAMlwzJxkhxgbfYwJJXXI3RIzqY0o8DnfurIhAcaSGdqgSbLvSul1szZ7fmrM0wXO+dpr+XPY9VnMENmVVptM/c=";
            _PublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjo1+KBcvwDSIo+nMYLeOJ19Ju4ii0xH66ZxFd869EWFWk/EJa3xIA2+4qGf/Ic7m7zi/NHuCnfUtUDmUdP0JfaZiYwn+1Ek7tYAOc1+1GxhzcexSJLyJlR2JLMfEM+rZooW4Ei7q3a8jdTWUNoak/bVPXnLEVLrbIguXABERQ0Ze0X9Fs0y/zkQFg8UjxUN88g2CRfMC6LldHm7UBo+d+WlpOYH7u0OTzoLLiP/04N1cfTgjjtqTBI7qkOGxYs6aBZHG1DJ6WdP+5w+ho91sBTVajsCxAaMoExWQM2ipf/1qGdsWmkZScPflBqg7m0olOD87ymAVP/3Tcbvi34bDfwIDAQAB";
        }

        public static string GetLocalIp()
        {
            IPAddress localIp = null;

            try
            {
                IPAddress[] ipArray;
                ipArray = Dns.GetHostAddresses(Dns.GetHostName());
                localIp = ipArray.First(ip => ip.AddressFamily == AddressFamily.InterNetwork);

            }
            catch (Exception ex)
            {
                //异常处理
            }
            if (localIp == null)
            {
                localIp = IPAddress.Parse("127.0.0.1");
            }
            return localIp.ToString();
        }
    }
}